%{
#include<cstdio>
#include<vector>
#include<iostream>
#include"astnodes.h"
extern int yylex();
extern int yyparse();
extern void yyerror(const char* s);
extern FILE* yyin;
extern std::vector<RegularExpressionNode*> root_exps;
%}

%union{
    char char_val;
    RegularExpressionNode* reg_node;
}
%token<char_val>CHAR
%token OR STAR LPAREN RPAREN SEMICOLON
%type<reg_node> Exp

%left OR
%left CONCAT
%precedence STAR
%precedence LPAREN RPAREN
%precedence CHAR
%%
lines   :       lines Exp SEMICOLON { @$=@1; root_exps.push_back($2); }
        |       Exp SEMICOLON {@$=@1; root_exps.push_back($1);}
        ;
Exp : CHAR {$$ = new RegularExpressionNode_char($1);}
    | Exp OR Exp {$$ = new RegularExpressionNode_or($1,$3);}
    | Exp Exp %prec CONCAT {$$ = new RegularExpressionNode_concat($1,$2);}
    | Exp STAR {$$ = new RegularExpressionNode_star($1);}
    | LPAREN Exp RPAREN {$$ = new RegularExpressionNode_paren($2);}
;
%%
void yyerror(const char* s){
    fprintf(stderr,"Parse error: %s\n",s);
    exit(1);
}
